from PIL import Image, ImageDraw
import numpy as np
import random

def generate_low_entropy_image(image_type, width=512, height=512):
    # 创建一个空白图像
    if image_type == 'L':  # 灰度图像
        image = Image.new('L', (width, height), color=0)
    elif image_type == 'RGB':  # RGB 图像
        image = Image.new('RGB', (width, height), color=(0, 0, 0))
    elif image_type == 'RGBA':  # RGBA 图像
        image = Image.new('RGBA', (width, height), color=(0, 0, 0, 255))
    elif image_type == 'P':  # 调色板图像
        image = Image.new('P', (width, height), color=0)
        # 创建一个简单的调色板
        palette = []
        for i in range(256):
            palette.extend([i, i, i])  # 灰度调色板
        image.putpalette(palette)
    else:
        raise ValueError(f"Unsupported image type: {image_type}")

    # 使用 ImageDraw 绘制形状
    draw = ImageDraw.Draw(image)

    # 随机生成形状的位置和大小
    rect_x1 = random.randint(0, width // 2)
    rect_y1 = random.randint(0, height // 2)
    rect_x2 = random.randint(width // 2, width)
    rect_y2 = random.randint(height // 2, height)

    circle_x1 = random.randint(0, width // 2)
    circle_y1 = random.randint(0, height // 2)
    circle_x2 = random.randint(circle_x1, width)
    circle_y2 = random.randint(circle_y1, height)

    # 随机生成颜色
    if image_type == 'P':
        # 对于调色板图像，使用调色板中的索引值作为颜色
        rect_color = random.randint(0, 255)  # 随机灰度值
        circle_color = random.randint(0, 255)  # 随机灰度值
        line_color1 = random.randint(0, 255)  # 随机灰度值
        line_color2 = random.randint(0, 255)  # 随机灰度值
    else:
        # 对于其他图像类型，使用随机 RGB 或灰度值
        rect_color = random.randint(0, 255) if image_type == 'L' else (
            random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        circle_color = random.randint(0, 255) if image_type == 'L' else (
            random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        line_color1 = random.randint(0, 255) if image_type == 'L' else (
            random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        line_color2 = random.randint(0, 255) if image_type == 'L' else (
            random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    # 绘制矩形
    draw.rectangle([rect_x1, rect_y1, rect_x2, rect_y2],
                   fill=rect_color, outline=line_color1)

    # 绘制圆形
    draw.ellipse([circle_x1, circle_y1, circle_x2, circle_y2],
                 fill=circle_color, outline=line_color2)

    # 绘制线条
    draw.line([(0, 0), (width, height)], fill=line_color1, width=5)
    draw.line([(0, height), (width, 0)], fill=line_color2, width=5)

    return image